package com.inspectortime.webapp;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.inspectortime.mail.service.AbstractMailMessage;
import com.inspectortime.mail.service.MailService;
import com.inspectortime.mail.service.StaticMailMessage;
import com.inspectortime.mail.service.StaticMailMessageTemplateNames;
import com.inspectortime.repository.Repository;
import com.inspectortime.util.SpringValidatorUtils;
import com.inspectortime.webapp.util.HttpSessionUtils;

/**
 * Simply sends an email to Inspector Time support.
 * If a user is logged in, we'll grab the users info and add it to the email
 * 
 * @author bones
 *
 */
public class SupportFormController extends SimpleFormController {
	
	private static Log log = LogFactory.getLog(SupportFormController.class);
	
	private String supportEmail;
	private MailService mailService;

	private Repository repository;


	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new SupportForm();
	}

	protected ModelAndView onSubmit(HttpServletRequest request,	HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		SupportForm form = (SupportForm) command;
		ValidationUtils.rejectIfEmpty(errors, "name", "required");
		SpringValidatorUtils.rejectInvalidEmail(errors, "email");
		ValidationUtils.rejectIfEmpty(errors, "email", "required");
		ValidationUtils.rejectIfEmpty(errors, "message", "required");
		

		if (errors.getErrorCount() > 0) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		
		
		//send an email
		Map<String, Object> props = new HashMap<String, Object>();
		props.put("name", form.getName());
		props.put("email", form.getEmail());
		props.put("message", form.getMessage());
		
		//if inspector is logged in, let's add some extra data to the email
		if (HttpSessionUtils.isUserLoggedIn(request)) {
			try {
				props.put("loggedInUser", HttpSessionUtils.retrieveLoggedInUser(request, repository));
			} catch (Exception e) {
				//Let's squash any exceptions so we should still send the email anymore
				log.error("Error trying to retrieve user info when sending email.  Ignoring.", e);
			}
		}

		String subject = "Inspector Time 'Contact Us' Support Request";
		AbstractMailMessage message = new StaticMailMessage(StaticMailMessageTemplateNames.CONTACT_US_SUPPORT, props, this.supportEmail, subject);
		message.setReplyTo(form.getEmail());
		this.mailService.send(message);
		
		ModelAndView mv = new ModelAndView(getSuccessView());
		mv.addObject("message", "We have received your message and will respond as soon as possible.  Thanks for using Inspector Time!");
		return mv;

	}
	
	public String getSupportEmail() {
		return supportEmail;
	}

	public void setSupportEmail(String supportEmail) {
		this.supportEmail = supportEmail;
	}

	public Repository getRepository() {
		return repository;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

}
